package studykana.database;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.zip.ZipInputStream;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Common implements Serializable {

	public static Common itseft;
	
	private static String databasePath;
	private static final long serialVersionUID = 1L;

	/**
	 * UnZip function, extract zipfile in package of application
	 */
	static {
		itseft = new Common();
	}

	@SuppressWarnings("resource")
	public void unZipFile(final String pathFile, final String unzipFilePath) {
		ZipInputStream in;
		try {
			in = new ZipInputStream(new BufferedInputStream(
					new FileInputStream(pathFile)));

			while (in.getNextEntry() != null) {
				FileOutputStream os = new FileOutputStream(unzipFilePath);
				byte[] buffer = new byte[1024 * 4];
				int r = 0;
				while ((r = in.read(buffer)) != -1) {
					os.write(buffer, 0, r);
				}

				in.closeEntry();
			}
			File fin = new File(pathFile);
			fin.delete();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void copyDBFromAssets(final String dbName, Context context) {
		final String dbPath = getDbPath(dbName, context);

		try {
			File file = new File(dbPath);
			if (file == null || file.length() <= 0) {
				InputStream in = context.getAssets().open(dbName);
				OutputStream out = new FileOutputStream(dbPath);

				byte[] buffer = new byte[in.available()];
				// read the text file as a stream, into the buffer
				in.read(buffer);
				// write this buffer to the output stream
				out.write(buffer);

				// Close the streams
				out.flush();
				out.close();
				in.close();
			}
		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	@SuppressLint("SdCardPath")
	private String getDbPath(final String dbName, Context context) {
		return "/data/data/" + context.getPackageName() + "/" + dbName;
	}

	public void initDatabase(final String dbName, Context context)
			throws Exception {
		databasePath = getDbPath(dbName, context);
		SQLiteDatabase db = null;

		db = SQLiteDatabase.openDatabase(databasePath, null,
				SQLiteDatabase.OPEN_READONLY
						| SQLiteDatabase.NO_LOCALIZED_COLLATORS);

		Databasehandler dbHander = new Databasehandler(context);
		dbHander.getAllAlphabet(db);
		db.close();

	}

	public SQLiteDatabase getDatabaseToRead(String dbName) {
		SQLiteDatabase db = null;
		db = SQLiteDatabase.openDatabase(databasePath, null,
				SQLiteDatabase.OPEN_READONLY
						| SQLiteDatabase.NO_LOCALIZED_COLLATORS);
		return db;
	}

	public SQLiteDatabase getDatabaseToWrite(String dbName) {
		SQLiteDatabase db = null;
		db = SQLiteDatabase.openDatabase(databasePath, null,
				SQLiteDatabase.OPEN_READWRITE
						| SQLiteDatabase.NO_LOCALIZED_COLLATORS);
		return db;
	}

	public Bitmap getBitmapFromAsset(String strName, Context context) {
		AssetManager assetManager = context.getAssets();
		InputStream istr = null;
		try {
			istr = assetManager.open(strName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Bitmap bitmap = BitmapFactory.decodeStream(istr);
		return bitmap;
	}
}
